/*
 * Task 1. Prime numbers
 */
import java.util.*;
import java.io.*;

/**
 * Finding a sequence of prime numbers
 * 
 * @author Vlad Oblamskyi
 */
public class PrimeNumbersFinder {
    private static final int FIRST_PRIME_NUMBER = 2;
    private static final int PRIME_SEARCH_STEP = 2;

    /**
     * Finding prime numbers which are less or equal to upper bound
     */
    public static List<Integer> getPrimeNumbers(int upperBound) {
        List<Integer> primeNumbers = new ArrayList<Integer>();

        primeNumbers.add(FIRST_PRIME_NUMBER);

        for (int currentNumber = FIRST_PRIME_NUMBER + 1; 
                 currentNumber <= upperBound; 
                 currentNumber += PRIME_SEARCH_STEP) {
            if (isPrime(currentNumber, primeNumbers)) {
                primeNumbers.add(currentNumber);
            }
        }

        return primeNumbers;
    }
    
    /*
     * Detecting if number is prime based on a list of all prime numbers,
     * which are less than number
     */
    private static boolean isPrime(int number, List<Integer> primeNumbers) {
        for (int index = 0; index < primeNumbers.size(); index++) {
            if (number % primeNumbers.get(index) == 0) {
                return false;
            }
        }
        return true;
    }

    public static int getInput(Scanner in) {
        System.out.print("Enter upper boundary (must be > 1): ");
        return in.nextInt();
    }
    
    public static void printResult(String result) {
        System.out.println("Result: " + result);
    }
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        int upperBound = getInput(in);
        List<Integer> primes = getPrimeNumbers(upperBound);

        printResult(Arrays.toString(primes.toArray()));
        
        in.close();
    }
}